package com.kl.nest;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class NestTest {

    private static final String FILEPATH = "src/main/java/com/kl/nest/my.xml";

    private static final String XML = "<Person>\n" +
            "    <firstName>super</firstName>\n" +
            "    <lastName>lkl</lastName>\n" +
            "    <phoneNumbers>\n" +
            "        <phoneNumbers>18386868888</phoneNumbers>\n" +
            "        <phoneNumbers>18386869999</phoneNumbers>\n" +
            "    </phoneNumbers>\n" +
            "    <address>\n" +
            "        <city>渝北区</city>\n" +
            "        <StreetName>洪湖东路</StreetName>\n" +
            "    </address>\n" +
            "    <address>\n" +
            "        <city>渝中区</city>\n" +
            "        <StreetName>八一路</StreetName>\n" +
            "    </address>\n" +
            "    <Students666>\n" +
            "        <students>\n" +
            "            <studentNo>1</studentNo>\n" +
            "            <studentName>Tom</studentName>\n" +
            "        </students>\n" +
            "        <students>\n" +
            "            <studentNo>2</studentNo>\n" +
            "            <studentName>Jerry</studentName>\n" +
            "        </students>\n" +
            "    </Students666>\n" +
            "    <Teacher>\n" +
            "        <students>\n" +
            "            <studentNo>3</studentNo>\n" +
            "            <studentName>lkl</studentName>\n" +
            "        </students>\n" +
            "        <students>\n" +
            "            <studentNo>4</studentNo>\n" +
            "            <studentName>man</studentName>\n" +
            "        </students>\n" +
            "    </Teacher>\n" +
            "</Person>";

    /**
     * 序列化列表属性
     */
    @Test
    public void test1() throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        Person person = testPerson(); // test data
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xmlMapper.writeValue(byteArrayOutputStream, person);
        byteArrayOutputStream.writeTo(new FileOutputStream(FILEPATH));
        System.out.println(byteArrayOutputStream);
    }

    private Person testPerson() {
        Person person = new Person();
        person.setFirstName("super");
        person.setLastName("lkl");
        List<Address> addresses = new ArrayList<>();
        Address address1 = new Address();
        address1.setStreetName("洪湖东路");
        address1.setCity("渝北区");
        Address address2 = new Address();
        address2.setStreetName("八一路");
        address2.setCity("渝中区");
        addresses.add(address1);
        addresses.add(address2);
        person.setAddress(addresses);
        List<String> phoneNumbers = new ArrayList<>();
        phoneNumbers.add("18386868888");
        phoneNumbers.add("18386869999");
        person.setPhoneNumbers(phoneNumbers);

        // 加入嵌套类集合
        List<Student> students = new ArrayList<>();
        Student student1 = new Student();
        student1.setStudentNo("1");
        student1.setStudentName("Tom");
        Student student2 = new Student();
        student2.setStudentNo("2");
        student2.setStudentName("Jerry");
        students.add(student1);
        students.add(student2);
        person.setStudents(students);

        // 类里面有集合
        Teacher teacher = new Teacher();
        List<Student> studentList = new ArrayList<>();
        Student student3 = new Student();
        student3.setStudentNo("3");
        student3.setStudentName("lkl");
        studentList.add(student3);
        Student student4 = new Student();
        student4.setStudentNo("4");
        student4.setStudentName("man");
        studentList.add(student4);
        // 添加
        teacher.setStudentList(studentList);
        person.setTeacher(teacher);

        return person;
    }

    /**
     * 反序列化列表属性
     */
    @Test
    public void test2() throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        String xmlContent = inputStreamToString(new FileInputStream(FILEPATH));
        Person value = xmlMapper.readValue(xmlContent, Person.class);
        System.out.println(value);
        assertEquals("渝北区", value.getAddress().get(0).getCity());
        assertEquals("渝中区", value.getAddress().get(1).getCity());
    }

    public String inputStreamToString(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }
}
